import re
from config import config
import os
import os.path


def read_svg(fileName):
    file_html = open(fileName, "rb")
    xml_data = file_html.read().decode('utf-8')
    matchObj = re.search(r'viewBox="(.*?) (.*?) (.*?) (.*?)"', xml_data)
    if matchObj:
        props = {
            "x_val": matchObj.group(1),
            "y_val": matchObj.group(2),
            "width": matchObj.group(3),
            "height": matchObj.group(4)
        }
        return props
    return


def set_config():
    imagesPath = "svg_test"
    list = os.listdir(imagesPath)
    for i in range(0, len(list)):
        path = os.path.join(imagesPath, list[i])
        if os.path.isfile(path):
            encoding = list[i].rstrip('.svg')
            props = read_svg(path)
            info = {"src": path, "width": props["width"]} if props else path
            config['glyphs'][encoding] = info
    return config
